package com.open.party.cloud.viewModel;

import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import com.open.party.cloud.ServerConfig
import com.open.party.cloud.model.MsgEntity
import com.open.party.cloud.model.MsgVo
import com.open.party.cloud.model.data.Api
import com.sinothk.rxretrofit.RxRetrofit
import org.greenrobot.eventbus.EventBus
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers


class MsgViewModel {

//    fun uploadFile(bizId: String, bizType: String, filePath: String) {
//
////        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
////            .create(BaseApi::class.java)
////            .uploadFile(bizId, bizType, RetrofitParam.createFileParam("file", File(filePath)))
////            .subscribeOn(Schedulers.io())
////            .observeOn(AndroidSchedulers.mainThread())
////            .subscribe(object : Subscriber<ResultInfo<Attachment>>() {
////                override fun onCompleted() {
////                }
////
////                override fun onError(e: Throwable) {
////                    EventBus.getDefault().post(Attachment())
////                }
////
////                override fun onNext(result: ResultInfo<Attachment>) {
////                    EventBus.getDefault().post(result.data)
////                }
////            })
//    }

    // 消息 》 分页列表
    fun getMsgPageListData(pageReq: PageReq<MsgVo>) {

        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getMsgPageListData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<MsgEntity>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<AppPageData<MsgEntity>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "getMsgPageListData"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<AppPageData<MsgEntity>>) {
                    result.eventType = "getMsgPageListData"
                    EventBus.getDefault().post(result)
                }
            })
    }

    // 消息 》 我的数据》分页列表
    fun getMsgPageListForMeData(pageReq: PageReq<MsgVo>) {

        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getMsgPageListForMeData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<MsgEntity>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<AppPageData<MsgEntity>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "getMsgPageListForMeData"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<AppPageData<MsgEntity>>) {
                    result.eventType = "getMsgPageListForMeData"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun getPartyLifeNoticeList(pageReq: PageReq<MsgVo>) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getPartyLifeNoticeList(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<MsgEntity>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<AppPageData<MsgEntity>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "getPartyLifeNoticeList"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<AppPageData<MsgEntity>>) {
                    result.eventType = "getPartyLifeNoticeList"
                    EventBus.getDefault().post(result)
                }
            })
    }
}
